package com.topnav.mc.admin.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;
import com.topnav.mc.admin.dao.mapper.DictionaryMapper;
import com.topnav.mc.admin.entity.Dictionary;
import com.topnav.mc.admin.entity.DictionaryValue;
import com.topnav.mc.admin.service.IDictionaryService;
import com.topnav.mc.admin.service.IDictionaryValueService;
import com.topnav.mc.base.CommServiceImpl;
@Service
public class DictionaryServiceImpl extends CommServiceImpl<DictionaryMapper,Dictionary> implements IDictionaryService{
	@Autowired
	private IDictionaryValueService dictionaryValueService;

	@Override
	public boolean beforeSave(Dictionary entity){
		if (this.getOne(new QueryWrapper<Dictionary>().eq("code",entity.getCode()), true) != null){
			throw new CustomException(-1,"字典类型  %s 已存在",entity.getCode());
		}
		return true;
	}

	@Override
	public boolean beforeUpdate(Dictionary entity){
		QueryWrapper<Dictionary> wrapper = new QueryWrapper<Dictionary>()
				.eq("code", entity.getCode()).ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			throw new CustomException(-1,"字典类型  %s 已存在",entity.getCode());
		}
		entity.setUpdateTime(new Date());
		return true;

	}

	@Override
	public boolean beforeDelete(Dictionary entity){
		DictionaryValue dv = new DictionaryValue().setDictId(entity.getId());
		List<DictionaryValue> list = dictionaryValueService.list(new QueryWrapper<>(dv));
		if (!CollectionUtils.isEmpty(list)){
			throw new CustomException(-1,"字典类型  %s 下已有数据，不能删除",entity.getCode());
		}
		return true;
	}

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<Dictionary> list(Wrapper<Dictionary> queryWrapper) {
		QueryWrapper<Dictionary> wrapper = new QueryWrapper<Dictionary>();
		if (queryWrapper.getEntity().getId()!=null){
			wrapper.eq("id", queryWrapper.getEntity().getId());
		}

		if (!StringUtils.isEmpty(queryWrapper.getEntity().getCode())){
			wrapper.eq("code", queryWrapper.getEntity().getCode());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getName())){
			wrapper.eq("name", queryWrapper.getEntity().getName());
		}

        return getBaseMapper().selectList(wrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<Dictionary>> E page(E page, Wrapper<Dictionary> queryWrapper) {
		QueryWrapper<Dictionary> wrapper = new QueryWrapper<Dictionary>();
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getCode())){
			wrapper.like("code", queryWrapper.getEntity().getCode());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getName())){
			wrapper.like("name", queryWrapper.getEntity().getName());
		}

        return getBaseMapper().selectPage(page, wrapper);
    }
}
